package cn.neusoft.controller;


import cn.neusoft.po.Dictype;
import cn.neusoft.query.DictypeQuery;
import cn.neusoft.service.IDictypeService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/dictype")
public class DictypeController {

    @Autowired
    IDictypeService service;

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Dictype dictype){
        boolean b = service.saveOrUpdate(dictype);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(90001,"保存字典类型信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(90002,"删除字典类型信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Dictype dictype = service.getById(id);
        return ResponseObj.SUCCESS(dictype);
    }

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody DictypeQuery query){
        //构建条件搜索对象
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.hasText(query.getName()),"name",query.getName());
        queryWrapper.like(StringUtils.hasText(query.getSn()),"sn",query.getSn());
        //构建分页条件对象
        Page pageQuery = new Page(query.getCurrent(),query.getSize());
        Page<Dictype> page = service.page(pageQuery, queryWrapper);
        return ResponseObj.SUCCESS(page);
    }

    @GetMapping("/all")
    public ResponseObj getAll(){
        List<Dictype> list = service.list();
        return ResponseObj.SUCCESS(list);
    }
    
}
